package com.project.erp.model.system;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.apache.struts2.json.annotations.JSON;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.project.erp.model.IdEntity;

/**
 * 
 * COPYRIGHT (C) 2013 BY XXX公司
 * 
 * 功能模块名：系统管理-部门管理
 * 
 * 功能描述：部门实体类
 * 
 * @author LiJiaGuo
 * 
 * @version 1.0
 * 
 * @created 2013-11-29 下午10:03:20
 */
@Entity
@Table(name = "TBL_DEPARTMENT")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Department extends IdEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6915801607784998607L;

	private String deptNum;// 部门编号
	private String deptName;// 部门名称
	private Department parentDept;// 上级部门
	private List<User> supervisors;// 部门主管
	private List<User> managers;// 部门经理
	private String deptPhone;// 部门电话
	private String description;// 部门描述
	private List<Posts> postList;// 岗位关联 ,一对多
	private String createUser;// 创建用户
	private Date createTime;// 创建时间
	private String updateUser;// 更新用户
	private Date updateTime;// 更新时间

	@Column(columnDefinition="varchar(30)")
	public String getDeptNum() {
		return deptNum;
	}

	public void setDeptNum(String deptNum) {
		this.deptNum = deptNum;
	}

	@Column(columnDefinition="varchar(80)")
	public String getDeptName() {
		return deptName;
	}

	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}

	@OneToOne
	@JoinColumn(columnDefinition="int")
	@org.hibernate.annotations.ForeignKey(name="fk_dept_super")
	@org.hibernate.annotations.Index(name="index_dept_super")
	public Department getParentDept() {
		return parentDept;
	}

	public void setParentDept(Department parentDept) {
		this.parentDept = parentDept;
	}

	//多对多定义
	@ManyToMany(fetch=FetchType.LAZY)
	//中间表定义,表名采用默认命名规则
	@JoinTable(name = "TBL_DEPT_USER_SUPERVISOR", joinColumns = {@JoinColumn(name = "DEPT_ID") }, inverseJoinColumns = { @JoinColumn(name = "USER_ID") })
	//Fecth策略定义
	@Fetch(FetchMode.SUBSELECT)
	//集合按id排序
	@OrderBy("id DESC")
	public List<User> getSupervisors() {
		return supervisors;
	}

	public void setSupervisors(List<User> supervisors) {
		this.supervisors = supervisors;
	}

	//多对多定义
	@ManyToMany(fetch=FetchType.LAZY)
	//中间表定义,表名采用默认命名规则
	@JoinTable(name = "TBL_DEPT_USER_MANAGER", joinColumns = {@JoinColumn(name = "DEPT_ID") }, inverseJoinColumns = { @JoinColumn(name = "USER_ID") })
	//Fecth策略定义
	@Fetch(FetchMode.SUBSELECT)
	//集合按id排序
	@OrderBy("id DESC")
	public List<User> getManagers() {
		return managers;
	}

	public void setManagers(List<User> managers) {
		this.managers = managers;
	}
	
	@Column(columnDefinition="varchar(30)")
	public String getDeptPhone() {
		return deptPhone;
	}

	public void setDeptPhone(String deptPhone) {
		this.deptPhone = deptPhone;
	}

	@Column(columnDefinition="varchar(300)")
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	//mappedBy="deptId"说明此字段由实体Posts的字段deptId映射而来，防止生成关联表
	@OneToMany(mappedBy="deptId")
	@Cascade(value = {CascadeType.DELETE})
	public List<Posts> getPostList() {
		return postList;
	}

	public void setPostList(List<Posts> postList) {
		this.postList = postList;
	}

	@Column(columnDefinition="varchar(30)")
	public String getCreateUser() {
		return createUser;
	}

	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}

	@JSON(format="yyyy-MM-dd")
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(columnDefinition="varchar(30)")
	public String getUpdateUser() {
		return updateUser;
	}

	public void setUpdateUser(String updateUser) {
		this.updateUser = updateUser;
	}

	@JSON(format="yyyy-MM-dd")
	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
}
